# -*- coding: utf-8 -*-
import random
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
import os

def draw_maze(num_rows, num_cols, M, image) -> list:
    """
    迷宫画图
    """
    M[0,0,0] = 1
    M[num_rows-1,num_cols-1,2] = 1

    for row in range(0,num_rows):
        for col in range(0,num_cols):
            cell_data = M[row,col]
            for i in range(10*row+2,10*row+8):
                image[i,range(10*col+2,10*col+8)] = 255
            if cell_data[0] == 1: 
                image[range(10*row+2,10*row+8),10*col] = 255
                image[range(10*row+2,10*row+8),10*col+1] = 255
            if cell_data[1] == 1: 
                image[10*row,range(10*col+2,10*col+8)] = 255
                image[10*row+1,range(10*col+2,10*col+8)] = 255
            if cell_data[2] == 1: 
                image[range(10*row+2,10*row+8),10*col+9] = 255
                image[range(10*row+2,10*row+8),10*col+8] = 255
            if cell_data[3] == 1: 
                image[10*row+9,range(10*col+2,10*col+8)] = 255
                image[10*row+8,range(10*col+2,10*col+8)] = 255
            
    
    # Display the image
    # plt.imshow(image, cmap = cm.Greys_r, interpolation='none')
    # plt.show()
    path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    plt.imsave(path + "/UI/maze.jpg", image, cmap = cm.Greys_r)

    return image

def draw_path(Path: list, OG_Map, Map) -> None:
    """
    路径画图
    """
    num_rows = len(OG_Map)
    num_cols = len(OG_Map)
    path_map = np.zeros((num_rows, num_cols), dtype=np.uint8)
    for i in Path:
        [row, col] = i
        if OG_Map[row, col] == 1:
            for j in range(6):
                for k in range(6):
                    path_map[row + j - 2, col + k - 2] = 100
    result_map = np.array([x if x != 0 and x < y else y for x, y in zip(np.array(path_map).reshape(-1).tolist(), np.array(Map).reshape(-1).tolist())] ).reshape(num_rows, num_cols).tolist()
    # plt.imshow(result_map, cmap = cm.Greys_r, interpolation='none')
    # plt.show()
    path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    plt.imsave(path + "/UI/result.jpg", result_map, cmap = cm.Greys_r)

